<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="代理商名称" prop="agentName">
        <el-input
          v-model="queryParams.agentName"
          placeholder="请输入代理商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="代理商号" prop="agentNo">
        <el-input
          v-model="queryParams.agentNo"
          placeholder="请输入代理商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select
          v-model="queryParams.state"
          placeholder=""
          clearable
        >
          <el-option
            v-for="dict in dict.type.paysystem_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['agent:edit']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['agent:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['agent:edit']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['agent:query']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="agentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="代理商号" align="center" prop="agentNo" />
      <el-table-column label="代理商名称" align="center" prop="agentName" />
      <el-table-column label="状态" align="center" prop="state" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.paysystem_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="代理商备注" align="center" prop="remark" />
      <el-table-column label="初始用户ID" align="center" prop="initUserId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['agent:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['agent:edit']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="resetGoogle(scope.row)"
            v-hasPermi="['agent:edit']"
          >重置谷歌</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改代理商信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="代理名称" prop="agentName">
          <el-input v-model="form.agentName" placeholder="请输入代理商名称" />
        </el-form-item>
        <el-form-item label="登陆账号" prop="loginUserName" v-if="!form.agentNo">
          <el-input v-model="form.loginUserName" placeholder="登陆账号" />
        </el-form-item>
        <el-form-item label="登陆密码" prop="loginPassword">
          <el-input v-model="form.loginPassword" placeholder="登陆密码(默认fusion666)" />
        </el-form-item>
        <el-form-item label="资金密码" prop="loginPassword">
          <el-input v-model="form.payPassword" placeholder="资金密码(默认fusion888)" />
        </el-form-item>
        <el-form-item label="TG群ID" prop="loginPassword">
          <el-input v-model="form.tgChatId" placeholder="" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="form.state">
            <el-radio
              v-for="dict in dict.type.paysystem_state"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="代理商备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入代理商备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAgent,
  getAgent,
  delAgent,
  addAgent,
  updateAgent,
  resetAgentGoogle
} from "@/api/fusion/agent";
export default {
  name: "Agent",
  dicts: ['paysystem_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 代理商信息表格数据
      agentList: [],
      agentIfList: [],
      inEditAgentNo: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openIf: false,
      openConfig: false,
      // 查询参数
      queryParams: {
        isAsc:'desc',
        orderByColumn:'agent_no',
        pageNum: 1,
        pageSize: 10,
        agentName: null,
        agentShortName: null,
        parentNo: null,
        state: null,
        initUserId: null,
      },
      agentConfig: {},
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        agentName: [
          { required: true, message: "代理商名称不能为空", trigger: "blur" }
        ],
        agentShortName: [
          { required: true, message: "代理商简称不能为空", trigger: "blur" }
        ],
        loginUserName: [
          { required: true, message: "登陆账号不能为空", trigger: "blur" }
        ],
        state: [
          { required: true, message: "代理商状态: 0-停用, 1-正常不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询代理商信息列表 */
    getList() {
      this.loading = true;
      listAgent(this.queryParams).then(response => {
        this.agentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        agentNo: null,
        agentName: null,
        agentShortName: null,
        parentNo: null,
        state: null,
        remark: null,
        tgChatId: null,
        initUserId: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.agentNo)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加代理商信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const agentNo = row.agentNo || this.ids
      getAgent(agentNo).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改代理商信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.agentNo != null) {
            updateAgent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAgent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    resetGoogle(row) {
      this.$modal.confirm('是否重置代理商的谷歌密钥').then(function() {
        return resetAgentGoogle(row.agentNo);
      }).then(() => {
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const agentNos = row.agentNo || this.ids;
      this.$modal.confirm('是否确认删除代理商信息编号为"' + agentNos + '"的数据项？').then(function() {
        return delAgent(agentNos);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('agent/export', {
        ...this.queryParams
      }, `agent_${new Date().getTime()}.xlsx`)
    },
    handleAgentPayIfUpdate(data) {
      let d = {}
      d.mpId = data.value.mpId
      d.agentNo = this.inEditAgentNo
      d.payifId = data.key.id
      d.rate = data.value.rate
      if(data.key.ifRate > d.rate) {
        this.$modal.msgError("代理商费率不能低于通道费率");
        return
      }
      updateAgentPayIf(d).then(response => {
        this.$modal.msgSuccess("修改成功");
        data.value = response.data
      });
    },
    handleAgentPayIfDelete(data) {
      deleteAgentPayIf(data.value.mpId).then(response => {
        this.$modal.msgSuccess("删除成功");
        data.value.mpId = null
        data.value.rate = ''
      });
    }
  }
};
</script>
